package com.qubuyer.business.auth.view

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import com.qubuyer.R
import com.qubuyer.business.auth.viewmodel.BindingViewModel
import com.qubuyer.constant.AppConstant
import com.qubuyer.core.app.MVVMActivity
import com.qubuyer.databinding.ActivityBindBinding
import com.qubuyer.utils.StatusBarUtil
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class BindingActivity : MVVMActivity<ActivityBindBinding, BindingViewModel>() {

    //选中的绑定第三方类型 0:微信 1:QQ
    private var mSelectedBindType = -1
    private var inCountDown = false

    //选中的第三方openid
    private var mSelectedBindOpenId: String? = null

    override val viewModel: BindingViewModel by injectVMByActivity()

    override fun onDataBind(dataBinding: ActivityBindBinding) {
        dataBinding.host = this
    }

    override val layoutId: Int
        get() = R.layout.activity_bind

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val layoutParams = dataBinding.ivBack.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.topMargin += StatusBarUtil.getStatusBarHeight()
        dataBinding.ivBack.layoutParams = layoutParams
        StatusBarUtil.setStatusBarLightMode(this, true)
        mSelectedBindType = intent.getIntExtra("bind_type", 1)
        mSelectedBindOpenId = intent.getStringExtra("bind_openid")

        dataBinding.etMobile.doAfterTextChanged {
            dataBinding.ivDelMobile.isVisible = !it.isNullOrEmpty()
            dataBinding.tvLogin.isEnabled = dataBinding.etCode.length() == 6 && dataBinding.etMobile.length() == 11
            if (!inCountDown) {
                dataBinding.tvGetCode.isEnabled = it?.length == 11
            }
        }
        dataBinding.etCode.doAfterTextChanged {
            dataBinding.ivDelCode.isVisible = !it.isNullOrEmpty()
            dataBinding.tvLogin.isEnabled = dataBinding.etCode.length() == 6 && dataBinding.etMobile.length() == 11
        }
        viewModel.getCodeResult.observe(this, {
            if (it) {
                toast("验证码获取成功")
                dataBinding.tvGetCode.isEnabled = false
                launch {
                    (60 downTo 0).asFlow().onEach { index ->
                        dataBinding.tvGetCode.text = "重新获取${index}S"
                        delay(1000)
                    }.collectLatest {
                        dataBinding.tvGetCode.text = "获取验证码"
                        dataBinding.tvGetCode.isEnabled = false
                    }
                }
            }
        })
        viewModel.thirdPartLoginResult.observe(this, {
            when (it) {
                AppConstant.CODE_GO_SET_PWD -> startActivityAndFinish(SetPwdActivity::class.java){ intent ->
                    intent.putExtra("token", viewModel.token)
                }
                200 -> finish()
            }
        })
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.ivDelCode -> dataBinding.etCode.setText("")
            R.id.ivDelMobile -> dataBinding.etMobile.setText("")
            R.id.tvGetCode -> {
                viewModel.getCode(dataBinding.etMobile.text.toString())
            }
            R.id.ivBack -> finish()
            R.id.tvLogin -> {
                val phone = dataBinding.etMobile.text.toString()
                val code = dataBinding.etCode.text.toString()
                viewModel.bindPhone(phone, code, mSelectedBindOpenId, if (mSelectedBindType == 0) "wx" else "qq")
            }
        }
    }
}